/*
 * @Author: sjf 1758142861@qq.com
 * @Date: 2023-03-28 15:51:57
 * @LastEditors: sjf 1758142861@qq.com
 * @LastEditTime: 2023-03-30 20:29:53
 * @FilePath: /sjf/lwp/block_queue/count_test.cc
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
#include "forward_blockqueue.hpp"
#include "count.hpp"
#include <cstdlib>
#include <time.h>

// const char* arr = "+-*/%";
// int myadd(int x, int y)
// {
//     return x + y;
// }

// // 生产者
// void *_producer(void *args)
// {
//     Anwtie::BlockQueue<Anwtie::Task> *queue = (Anwtie::BlockQueue<Anwtie::Task> *)(args);
//     while (true)
//     {
//         sleep(1);
//         int x = rand() % 10240;
//         int y = rand() % 11111;
//         char op = arr[rand()%5];
//         Anwtie::Task t(x,y,op,myadd);
//         queue->push(t);
//         //std::cout << "producer->push_data:" << data << std::endl;
//     }
//     return nullptr;
// }

// // 消费者
// void *_consumer(void *args)
// {
//     Anwtie::BlockQueue<Anwtie::Task> *queue = (Anwtie::BlockQueue<Anwtie::Task> *)(args);
//     while (true)
//     {
//         sleep(1);
//         Anwtie::Task t;
//         //int data;
//         queue->pop(&t);
//         std::cout << "consumer->pop_data:" << t() << std::endl;
//     }
//     return nullptr;
// }

// int main()
// {
//     srand((unsigned int)time(nullptr));
//     pthread_t producer, consumer;
//     Anwtie::BlockQueue<Anwtie::Task> *queue = new Anwtie::BlockQueue<Anwtie::Task>;
//     pthread_create(&producer, nullptr, _producer, (void *)queue);
//     pthread_create(&consumer, nullptr, _consumer, (void *)queue);

//     pthread_join(producer, nullptr);
//     pthread_join(consumer, nullptr);

//     delete queue;
//     return 0;
// }

/**
 * 基于.hpp中的Anwite_task空间中的实现测试
 */
// 生产者
void *_producer(void *args)
{
    Anwtie::BlockQueue<Anwite_task::Task<int>> *queue = (Anwtie::BlockQueue<Anwite_task::Task<int>> *)(args);
    const char *arr = "+-*/%";
    while (true)
    {
        //sleep(1);
        int x = rand() % 10000;
        int y = rand() % 11111;
        char op = arr[rand() % 5];
        Anwite_task::Task<int> t(x, y, op);
        t.is_why_test();
        queue->push(t);
    }
    return nullptr;
}

// 消费者
void *_consumer(void *args)
{
    Anwtie::BlockQueue<Anwite_task::Task<int>> *queue = (Anwtie::BlockQueue<Anwite_task::Task<int>> *)(args);
    while (true)
    {
        //sleep(1);
        Anwite_task::Task<int> t;
        queue->pop(&t);
        t.Runc();
    }
    return nullptr;
}

int main()
{
    srand((unsigned int)time(nullptr));
    pthread_t producer, consumer;
    Anwtie::BlockQueue<Anwite_task::Task<int>> *queue = new Anwtie::BlockQueue<Anwite_task::Task<int>>;
    pthread_create(&producer, nullptr, _producer, (void *)queue);
    pthread_create(&consumer, nullptr, _consumer, (void *)queue);

    pthread_join(producer, nullptr);
    pthread_join(consumer, nullptr);

    delete queue;
    return 0;
}

// int main()
// {
//     srand((unsigned int)time(nullptr));
//     pthread_t p[2], c[3];
//     Anwtie::BlockQueue<Anwite_task::Task<int>> *queue = new Anwtie::BlockQueue<Anwite_task::Task<int>>;
//     pthread_create(p, nullptr, _producer, (void *)queue);
//     pthread_create(p+1, nullptr, _consumer, (void *)queue);
//     pthread_create(c, nullptr, _consumer, (void *)queue);
//     pthread_create(c+1, nullptr, _consumer, (void *)queue);
//     pthread_create(c+2, nullptr, _consumer, (void *)queue);

//     pthread_join(p[0], nullptr);
//     pthread_join(p[1], nullptr);
//     pthread_join(c[0], nullptr);
//     pthread_join(c[1], nullptr);
//     pthread_join(c[2], nullptr);

//     delete queue;
//     return 0;
// }